home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / clx.lha / clx / socket.l < prev    next >
Text File  |  1988-09-12  |  2KB  |  94 lines

  1. /*
  2.  * THIS IS AN OS DEPENDENT FILE! It should work on 4.2BSD derived
  3.  * systems.  VMS and System V should plan to have their own version.
  4.  *
  5.  * This code was cribbed from X11 beta connection code in XLIB.
  6.  * Compile using   
  7.  *                    % cc -c socket.c
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <errno.h>
  13. #include <netinet/in.h>
  14. #include <sys/ioctl.h>
  15. #include <netdb.h> 
  16. #include <fcntl.h>
  17. #include <sys/socket.h>
  18. #include <strings.h>
  19.  
  20. void bcopy();
  21.  
  22. /* 
  23.  * Attempts to connect to server, given host and display. Returns file 
  24.  * descriptor (network socket) or 0 if connection fails.
  25.  */
  26.  
  27. int connect_to_server (host, display)
  28.      char *host;
  29.      int display;
  30. {
  31.   struct sockaddr_in inaddr;    /* INET socket address. */
  32.   struct sockaddr *addr;        /* address to connect to */
  33.   struct hostent *host_ptr;
  34.   int addrlen;            /* length of address */
  35.   extern char *getenv();
  36.   extern struct hostent *gethostbyname();
  37.   int fd;                /* Network socket */
  38.   {
  39.     {
  40.       /* Get the statistics on the specified host. */
  41.       if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1) 
  42.     {
  43.       if ((host_ptr = gethostbyname(host)) == NULL) 
  44.         {
  45.           /* No such host! */
  46.           errno = EINVAL;
  47.           return(-1);
  48.         }
  49.       /* Check the address type for an internet host. */
  50.       if (host_ptr->h_addrtype != AF_INET) 
  51.         {
  52.           /* Not an Internet host! */
  53.           errno = EPROTOTYPE;
  54.           return(-1);
  55.         }
  56.       /* Set up the socket data. */
  57.       inaddr.sin_family = host_ptr->h_addrtype;
  58.       bcopy((char *)host_ptr->h_addr, 
  59.         (char *)&inaddr.sin_addr, 
  60.         sizeof(inaddr.sin_addr));
  61.     } 
  62.       else 
  63.     {
  64.       inaddr.sin_family = AF_INET;
  65.     }
  66.       addr = (struct sockaddr *) &inaddr;
  67.       addrlen = sizeof (struct sockaddr_in);
  68.       inaddr.sin_port = display;
  69.       inaddr.sin_port = htons(inaddr.sin_port);
  70.       /*
  71.        * Open the network connection.
  72.        */
  73.       if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0){
  74.     return(-1);        /* errno set by system call. */}
  75.       /* make sure to turn off TCP coalescence */
  76. #ifdef TCP_NODELAY
  77.       {
  78.     int mi;
  79.     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int));
  80.       }
  81. #endif
  82.     }
  83.     if (connect(fd, addr, addrlen) == -1) 
  84.       {
  85.     (void) close (fd);
  86.     return(-1);         /* errno set by system call. */
  87.       }
  88.   }
  89.   /*
  90.    * Return the id if the connection succeeded.
  91.    */
  92.   return(fd);
  93. }
  94.